<template>
  <a-modal :title="title" :width="800" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk"
    @cancel="handleCancel" :cancelText="$t('systemManagement.close')" wrapClassName="ant-modal-cust-warp"
    style="top:30%;height: 40%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        {{ $t('systemManagement.close') }}
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" id="personModal">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('basicInformation.fullName')">
          <a-input :placeholder="$t('inPrint.pleaseYourName')" v-decorator.trim="['name', validatorRules.name]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('basicInformation.department')">
          <a-select :placeholder="$t('basicInformation.pleaseType')" v-decorator="['type', validatorRules.type]">
            <a-select-option :value="'admin'">admin</a-select-option>
            <a-select-option :value="'it 部门'">it 部门</a-select-option>
            <a-select-option :value="'purchasing'">purchasing</a-select-option>
            <a-select-option :value="'transport'">transport</a-select-option>
            <a-select-option :value="'general'">general</a-select-option>
            <a-select-option :value="'operator'">operator</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { addPerson, editPerson, checkPerson } from '@/api/api'
import { autoJumpNextInput } from "@/utils/util"
export default {
  name: "PersonModal",
  data() {
    return {
      title: this.$t('setailManagement.operation'),
      visible: false,
      model: {},
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        name: {
          rules: [
            { required: true, message: this.$t('inPrint.pleaseYourName') + '!' },
            { min: 2, max: 30, message: this.$t('systemManagement.length2and30'), trigger: 'blur' },
            { validator: this.validatePersonName }
          ]
        },
        type: {
          rules: [
            { required: true, message: this.$t('basicInformation.pleaseType') + '!' }
          ]
        }
      },
    }
  },
  created() {
  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'name', 'type', 'description'))
        autoJumpNextInput('personModal')
      });
    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let formData = Object.assign(this.model, values);
          let obj;
          if (!this.model.id) {
            obj = addPerson(formData);
          } else {
            obj = editPerson(formData);
          }
          obj.then((res) => {
            if (res.code === 200) {
              that.$emit('ok');
            } else {
              that.$message.warning(res.data.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validatePersonName(rule, value, callback) {
      let params = {
        name: value,
        id: this.model.id ? this.model.id : 0
      };
      checkPerson(params).then((res) => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback();
          } else {
            callback(this.$t('commodityManagement.nameExists'));
          }
        } else {
          callback(res.data);
        }
      });
    }
  }
}
</script>
<style scoped>

</style>